﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
using System.Net;
using System.IO;
using Engine.Dtd.Schema;

namespace Engine.Sgml.Test {
    [TestClass]
    public class DocumentLoadTest {
        /// <summary>
        ///A test for Load
        ///</summary>
        [TestMethod()]
        public void LoadTest() {
            SgmlDocument target = new SgmlDocument(); // TODO: Initialize to an appropriate value
            Uri url = new Uri(@"C:\Documents and Settings\Kir\Рабочий стол\SGML Document\Engine.Sgml.Test\Document Samples\Html Agility Pack Incorrect parsing of HTML4 optional end tags.html");
            target.Load(url);

            string content = target.ToString(false);
            }

        [TestMethod]
        public void LoadGoogleStartPage() {
            DtdSchema schema = DtdSchema.Load(new Uri(@"C:\Documents and Settings\Kir\Рабочий стол\SGML Document\Engine.Sgml.Test\Scheme Samples\Xhtml1-strict.dtd"), "html");
            SgmlDocument document = new SgmlDocument(schema);

            Uri address = new Uri("http://google.com");
            document.Load(address);

            SElement node = document.SelectElement(".//*[@id]");

            string content = document.ToString();
            string xml = document.ToXml();
            }

        [TestMethod]
        public void LoadSpecifiedDocumentFromInternet() {
            Uri address = new Uri("http://www.robotstxt.org/guidelines.html");
            SgmlDocument document = new SgmlDocument();
            document.Load(address);

            //string xml = document.ToXml();
            string content = document.ToString(false);
            }

        [TestMethod]
        public void LoadDocumentsFromInternetTest() {
            // Start address: http://twitter.com/BabyCenter

            SgmlDocument google = new SgmlDocument();
            SgmlDocument document = new SgmlDocument();
            SgmlDocument doc = new SgmlDocument();

            google.TraceSource.Switch.Level = SourceLevels.All;
            UrlResolver resolver = new UrlResolver();
            Uri address = new Uri("http://www.google.com.ua/");
            google.Load(new Uri(address, "search?q=Freedom"));
            int pageIndex = 1;
            do {
                IEnumerable<SElement> links = google.SelectElements("html/body//p/a");

                foreach (SElement anchor in links) {
                    Uri uri = new Uri(address, anchor.Attribute("href").Value);
                    try {
                        Debug.Print(uri.ToString());
                        document.Load(uri);
                        Debug.Print(document.Url.ToString());
                        SaveDocument(document);
                        foreach (SElement a in document.SelectElements(".//a[@href]")) {
                            try {
                                SAttribute href = a.Attribute("href");
                                Assert.IsNotNull(href, "The HREF attribute was not found.");
                                if (!Uri.IsWellFormedUriString(href.Value, UriKind.RelativeOrAbsolute))
                                    continue;
                                Assert.IsNotNull(document.Url);
                                uri = new Uri(document.Url, href.Value);
                                if (Path.GetExtension(uri.LocalPath).Equals(".pdf", StringComparison.OrdinalIgnoreCase) ||
                                    Path.GetExtension(uri.LocalPath).Equals(".exe", StringComparison.OrdinalIgnoreCase))
                                    continue;
                                if (!(uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps))
                                    continue;
                                }
                            catch (UriFormatException) {
                                continue;
                                }
                            catch (ArgumentException e) {
                                Debug.Print(e.ToString());
                                continue;
                                }
                            try {
                                Debug.Print(uri.ToString());
                                doc.Load(uri);
                                SaveDocument(doc);
                                Debug.Print(doc.Url.ToString());
                                }
                            catch (IOException) {
                                continue;
                                }
                            catch (WebException) {
                                continue;
                                }
                            }
                        }
                    catch (IOException) {
                        continue;
                        }
                    catch (WebException) {
                        continue;
                        }
                    }
                Debug.Print("Page number: {0}", pageIndex++);
                SElement nextPage = google.SelectElement(".//a[strong[text() = 'Уперед']]");
                if (nextPage == null)
                    break;
                google.Load(new Uri(address, nextPage.Attribute("href").Value));
                } while (true);
            }

        private void SaveDocument(SgmlDocument document) {
            //Encoding encoding = document.Encoding;
            //if (encoding == null)
            //    encoding = document.Settings.DefaultEncoding;
            //File.WriteAllText(Path.Combine(@"D:\Saved SGML Documents", string.Concat(Path.GetRandomFileName(), ".html")), document.ToString(), encoding);
            }

        [TestMethod]
        public void LoadDocumentAndSave() {
            HttpRequestSettings settings = HttpRequestSettings.DefaultSettings;
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(@"http://www.freedom.crimea.ua/"));
            webRequest.UserAgent = settings.UserAgent;
            webRequest.Timeout = settings.Timeout;
            webRequest.Proxy = settings.WebProxy;
            // Pass the cookie associated with request.
            webRequest.CookieContainer = settings.CookieContainer;
            webRequest.PreAuthenticate = false;
            // Pass the credentials of the process. 
            webRequest.Credentials = CredentialCache.DefaultCredentials;

            WebResponse webResponse = webRequest.GetResponse();
            Stream responseStream = webResponse.GetResponseStream();
            //Encoding encoding = Encoding.GetEncoding("ISO-8859-1");
            Encoding encoding = Encoding.GetEncoding(1251);
            //Encoding encoding = Encoding.UTF8;
            StreamReader reader = new StreamReader(responseStream, encoding);
            //string fileName = Path.GetRandomFileName();
            string fileName = "Frame Declaration Null Error.xml";
            File.WriteAllText(Path.Combine(@"C:\Documents and Settings\Kir\Рабочий стол\SGML Document\Engine.Sgml.Test\Document Samples", fileName), reader.ReadToEnd(), encoding);
            Debug.Print(fileName);
            }
        }
    }
